#pragma once
#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <cstring>

#include <cstdlib>
#include <errno.h>
#include <sys/shm.h>

#include "logtxt.hpp"

using namespace std;
const int size = 4096;
const string pathname = "/home/TCY";
const int proj_id = 0x6666;
Log log;

key_t Get_key()
{
    key_t k = ftok(pathname.c_str(), proj_id);

    if (k < 0)
    {
        log(fatal, " ftok fail :%s", strerror(errno));
        exit(1);
    }
    log(Info, " ftok scussess,key is: ox%x", k);
    return k;
}

int GetShareMemHelper(int flag)
{
    key_t k = Get_key();

    int shmid = shmget(k, size, flag);

    if (shmid < 0)
    {
        log(fatal, "creat shmget fail: %s", strerror(errno));
        exit(2);
    }
    log(Info, "creat shmget scussess,shmid: %d", shmid);
    return shmid;
}

int Creat_shamem()
{
    return GetShareMemHelper(IPC_CREAT | IPC_EXCL | 0666);
}
int Get_shamem()
{
    return GetShareMemHelper(IPC_CREAT);
}
